package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import com.atguigu.lease.common.exception.LeaseException;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    /*
     *  1.修改
     *  2.添加
     * */
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;


    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;


    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;


    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateInfo(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId(); //公寓的id

        // 再添加之前查询到 省市区
        // 查询公寓所属的省的信息(名字) 已知: provinceId
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        // 查询公寓所属的城市名字
        CityInfo cityInfo = cityInfoMapper.selectById(apartmentSubmitVo.getCityId());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        //查询 公寓所属的区县名字
        DistrictInfo districtInfo = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        //添加或者修改公寓的基本信息
        this.saveOrUpdate(apartmentSubmitVo);

        // 是否是修改
        if (id != null) { // 是修改->先删除和当前公寓有关联的外键表信息 然后再添加最新的关联信息
            // 根据公寓id  删除公寓配套关系表信息
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                    .eq(ApartmentFacility::getApartmentId, id));
            // 根据公寓id  删除公寓标签关系表信息
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                    .eq(ApartmentLabel::getApartmentId, id));
            // 根据公寓id  删除公寓杂费关系表信息
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                    .eq(ApartmentFeeValue::getApartmentId, id));
            //根据公寓id  删除公寓图片表信息
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                    // GraphInfo::getItemId 获取公寓id
                    .eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.APARTMENT));
        }

        // 添加最新的公寓配套关系表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();  // 获取配套id集合
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {  // 集合不为null 执行
            ArrayList<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())  // 获取 最新的公寓id
                        .facilityId(facilityInfoId)
                        .build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList); // 批量添加
        }

        // 添加最新的公寓标签关系表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId)
                        .build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        // 添加公寓杂费关系
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId)
                        .build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        // 添加最新的公寓图片信息
        //添加公寓的图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfos=new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo=new GraphInfo();

                String url = graphVo.getUrl();

                String fileName = url.substring(url.lastIndexOf("/")+1);
                graphInfo.setName(fileName);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItem(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItem(apartmentItemVoPage,queryVo);
    }


    // 回显的方法：根据公寓的id查询公寓的基本信息 以及 相关信息
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        // 1、查询ApartmentInfo 公寓的基本信息
        // this 代表当前类对象 ApartmentInfoService
        ApartmentInfo apartmentInfo = this.getById(id);  // 放回 的是 一个包含公寓基本信息的Info对象

        // 2、查询公寓的配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        // 3、查询公寓的标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        // 3、查询公寓的杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        // 4、查询公寓的图片
        List<GraphVo> graphVoList = graphInfoMapper.selectListByApartmentId(id,ItemType.APARTMENT);

        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVoList);

        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentByid(Long id) {
        // 查询当前被删除的公寓是否还有房间 ， 如果有房间提示不能删除
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count > 0){
            // 有房间，提示不能删除 抛自定义异常
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        // 删除公寓信息
        this.removeById(id);

        // 删除公寓相关信息
        // 根据公寓id  删除公寓配套关系表信息
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                .eq(ApartmentFacility::getApartmentId, id));
        // 根据公寓id  删除公寓标签关系表信息
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                .eq(ApartmentLabel::getApartmentId, id));
        // 根据公寓id  删除公寓杂费关系表信息
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                .eq(ApartmentFeeValue::getApartmentId, id));
        //根据公寓id  删除公寓图片表信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                // GraphInfo::getItemId 获取公寓id
                .eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.APARTMENT));

    }

}




